﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
namespace Data
{
   public class ChiTietBaoCaoCongNoRepository
    {
       public static List<ChiTietBaoCaoCongNoDTO> GetChiTietBaoCaoList(int maBaoCao)
       {
           NhaSachEntities context = new NhaSachEntities();
           var q = (from ct in context.ChiTietBaoCaoCongNoList
                    where ct.MaBaoCaoCongNo == maBaoCao
                    orderby ct.MaKhachHang
                    select new ChiTietBaoCaoCongNoDTO()
                    {
                        MaChiTiet = ct.MaChiTiet,
                        MaKhachHang = (int)ct.MaKhachHang,
                        MaBaoCaoCongNo = (int)ct.MaBaoCaoCongNo,
                        NoDau = (double)ct.NoDau,
                        NoCuoi = (double)ct.NoCuoi,
                        PhatSinh = (double)ct.PhatSinh
                    }).DefaultIfEmpty();
           return q.ToList();
       }
        public static ChiTietBaoCaoCongNoDTO GetChiTietBaoCao(int maBaoCao, int maKhachHang)
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = (from ct in context.ChiTietBaoCaoCongNoList
                     where ct.MaBaoCaoCongNo == maBaoCao && ct.MaKhachHang == maKhachHang
                     select new ChiTietBaoCaoCongNoDTO()
                     {
                         MaChiTiet = ct.MaChiTiet,
                         MaKhachHang = (int)ct.MaKhachHang,
                         MaBaoCaoCongNo = (int)ct.MaBaoCaoCongNo,
                         NoDau = (double)ct.NoDau,
                         NoCuoi = (double)ct.NoCuoi,
                         PhatSinh = (double)ct.PhatSinh
                     }).FirstOrDefault();
            return q;
        }
        public static bool Exists(int maBaoCao, int maKhachHang)
        {
            NhaSachEntities context = new NhaSachEntities();

            var q = (from ct in context.ChiTietBaoCaoCongNoList
                     where ct.MaBaoCaoCongNo == maBaoCao && ct.MaKhachHang == maKhachHang
                     select ct).Count();
            if (q > 0)
                return true;
            return false;
        }
        public static void Insert(ChiTietBaoCaoCongNoDTO chiTietBaoCao)
        {
            ChiTietBaoCaoCongNo ct = new ChiTietBaoCaoCongNo()
            {
                MaBaoCaoCongNo = chiTietBaoCao.MaBaoCaoCongNo,
                MaKhachHang = chiTietBaoCao.MaKhachHang,
                NoDau = chiTietBaoCao.NoDau,
                PhatSinh = chiTietBaoCao.PhatSinh,
                NoCuoi = chiTietBaoCao.NoCuoi
            };
            NhaSachEntities context = new NhaSachEntities();
            context.AddToChiTietBaoCaoCongNoList(ct);
            context.SaveChanges();
        }
        public static void Update(ChiTietBaoCaoCongNoDTO chiTietBaoCao)
        {
            NhaSachEntities context = new NhaSachEntities();
            var chiTiet = (from ct in context.ChiTietBaoCaoCongNoList
                           where chiTietBaoCao.MaChiTiet == ct.MaChiTiet
                           select ct).FirstOrDefault();
            chiTiet.NoDau = chiTietBaoCao.NoDau;
            chiTiet.PhatSinh = chiTietBaoCao.PhatSinh;
            chiTiet.NoCuoi = chiTietBaoCao.NoCuoi;

            context.SaveChanges();
        }
    }
}
